{% extends 'window.twig' %}
{% block body %}
    <div class="admin-main-container">
        <form action="model.field.add?act={{ mode }}" method="post" class="data-validate form-horizontal data-view" post-type="ajax">
            <input type="hidden" name="model_id" value="{{ modelId }}"/>
            {% if mode == 'update' %}
            <input type="hidden" name="field_id" value="{{ fieldData.field_id }}"/>
            {% endif %}
            <div class="with-padding">
                <div class="form-item">
                    <div class="item-label">
                        字段标识<span class="check-tips">（标识唯一不能重复且字母开头,不能有中文和特殊符号）</span>
                        <label class="error" for="identifier"></label>
                    </div>
                    <div class="controls">
                        <input type="text" data-rules="required remote" data-message-required="请输入字段标识"
                               remote-url="model.field.check?id={{ modelId }}{{ mode=='update'?'&fid=':'' }}{{ mode=='update'?fieldData.field_id:'' }}" name="identifier"
                               class="form-control" value="{{ fieldData.identifier }}">
                    </div>
                </div>
                <div class="form-item">
                    <div class="item-label">
                        字段名称<span class="check-tips"></span>
                        <label class="error" for="title"></label>
                    </div>
                    <div class="controls">
                        <input type="text" class="form-control" data-rules="required"
                               data-message-required="请输入字段名称" name="title" value="{{ fieldData.title }}">
                    </div>
                </div>
                <div class="form-item">
                    <div class="item-label">单位<span class="check-tips"></span></div>
                    <div class="controls">
                        <input type="text" class="form-control" name="unit" value="{{ fieldData.unit }}">
                    </div>
                </div>
                <div class="form-item">
                    <div class="item-label">是否允许为空<span class="check-tips"></span></div>
                    <div class="radio">
                        <label><input type="radio" name="allow_null" value="1" {% if mode == 'insert' or fieldData.allow_null == 1 %}checked="checked"{% endif %}>是</label>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <label><input type="radio" name="allow_null" value="0" {{ mode == 'update' and fieldData.allow_null == 0 ? 'checked="checked"':'' }}>否</label>
                    </div>
                </div>
                <div class="form-item">
                    <div class="item-label">字段类型<span class="check-tips"></span></div>
                    <div class="controls">
                        <select name="type" id="field_type_slt" class="form-control">
                            {% for ak,at in typeList %}
                            <option value="{{ ak }}"{{ fieldData.type == ak ? ' selected="selected"' :'' }}>{{ at }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div id="field_type_model_wrapper"></div>
                <div class="form-item">
                    <div class="item-label">字段描述<span class="check-tips"></span></div>
                    <div class="controls">
                        <textarea class="form-control" name="description">{{ fieldData.description }}</textarea>
                    </div>
                </div>
            </div>

            <div class="form-item with-padding" style="margin-top: 10px;">
                <button class="btn btn-primary" type="submit">确 定</button>
                <a class="btn btn-info" href="model.fields?id={{ modelId }}">返 回</a>
            </div>
        </form>
        <div class="field-type-model" style="display: none;">
            <div class="item model-type" type="text"></div>
            <div class="item model-type" type="number"></div>
            <div class="item model-type" type="radio">
                <div class="form-item">
                    <div class="item-label">可选项内容
                        <span class="check-tips">(每行一个字段，等号前面为字段索引,后面为内容)</span>
                        <label class="error" for="rules[choice]"></label>
                    </div>
                    <div class="controls">
                        <textarea class="form-control" name="rules[choice]" data-rules="required"
                                  data-message-required="请输入可选项内容,每行一个">{{ mode == 'update' and fieldData.type=='radio' ? fieldData.rules.choice :'' }}</textarea>
                    </div>
                </div>
            </div>
            <div class="item model-type" type="select">
                <div class="form-item">
                    <div class="item-label">可选项内容
                        <span class="check-tips">(每行一个字段，等号前面为字段索引,后面为内容)</span>
                        <label class="error" for="rules[choice]"></label>
                    </div>
                    <div class="controls">
                        <textarea class="form-control" name="rules[choice]" data-rules="required"
                                  data-message-required="请输入可选项内容,每行一个">{{ mode == 'update' and fieldData.type == 'select' ? fieldData.rules.choice :'' }}</textarea>
                    </div>
                </div>
            </div>
            <div class="item model-type" type="textarea"></div>
            <div class="item model-type" type="datetime"></div>
        </div>
    </div>
{% endblock %}
{% block script %}
    <script type="text/javascript">
        $(function(){
            var changeModel = function(){
                var type = $(this).val();
                var mtmw = $("#field_type_model_wrapper");
                mtmw.html('');
                var m = $('.model-type[type='+type+']');
                if(m.length > 0) m.clone().appendTo(mtmw);
            };
            $('#field_type_slt').on('change',changeModel);
            changeModel.call($('#field_type_slt'));
        });
    </script>
{% endblock %}